public class Something {

    public static int lengthOfLastWord(String s) {
        if (s.length() == 1) {
            return 1;
        }
        int last = s.length() - 1;
        int ans = 0;
        while (last >= 0 && s.charAt(last) == ' ') {
            --last;
        }
        if (last < 0) {
            return 0;
        }

        while (s.charAt(last) != ' ') {
            ++ans;
            --last;
        }
        return ans;
    }

    public static void main(String[] args) {
        int res = lengthOfLastWord("a ");
        System.out.println(res);
    }
}

/*
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        return merge(lists, 0, lists.length - 1);
    }

    public ListNode merge(ListNode[] lists, int l, int r) {
        if (l == r) {
            return lists[l];
        }
        if (l > r) {
            return null;
        }
        int mid = l + ((r - l) >> 1);
        return mergeLists(merge(lists, l, mid),merge(lists, mid + 1, r));
    }

    public ListNode mergeLists(ListNode o1, ListNode o2) {
        if (o1 == null || o2 == null) {
            return o1 == null ? o2 : o1;
        }
        ListNode newHead = new ListNode(-1);
        ListNode p1 = o1, p2 = o2, tail = newHead;
        while (p1 != null && p2!= null) {
            if (p1.val < p2.val) {
                tail.next = p1;
                p1 = p1.next;
            } else {
                tail.next = p2;
                p2 = p2.next;
            }
            tail = tail.next;
        }
        tail.next = (p1 == null) ? p2 : p1;
        return newHead.next;
    }
}
 */


/*
class Solution {
    public int lengthOfLastWord(String s) {
        if (s.length() == 1) {
            return 1;
        }
        int last = s.length() - 1;
        int ans = 0;
        while (last >= 0 && s.charAt(last) == ' ') {
                --last;
        }
        if (last < 0) {
            return 0;
        }

        while (last >= 0 && s.charAt(last) != ' ') {
            ++ans;
            --last;
        }
        return ans;
    }
}
 */

/*
public static int getMaxLength(int[] arr, int K) {
		if (arr == null || arr.length == 0 || K <= 0) {
			return 0;
		}
		int left = 0;
		int right = 0;
		int sum = arr[0];
		int len = 0;
		while (right < arr.length) {
			if (sum == K) {
				len = Math.max(len, right - left + 1);
				sum -= arr[left++];
			} else if (sum < K) {
				right++;
				if (right == arr.length) {
					break;
				}
				sum += arr[right];
			} else {
				sum -= arr[left++];
			}
		}
		return len;
	}

 */

/*
public static int max1(int[] arr) {
		int max = Integer.MIN_VALUE;
		for (int i = 0; i < arr.length; i++) {
			for (int j = i; j < arr.length; j++) {
				int minNum = Integer.MAX_VALUE;
				int sum = 0;
				for (int k = i; k <= j; k++) {
					sum += arr[k];
					minNum = Math.min(minNum, arr[k]);
				}
				max = Math.max(max, minNum * sum);
			}
		}
		return max;
	}

	public static int max2(int[] arr) {
		int size = arr.length;
		int[] sums = new int[size];
		sums[0] = arr[0];
		for (int i = 1; i < size; i++) {
			sums[i] = sums[i - 1] + arr[i];
		}
		int max = Integer.MIN_VALUE;
		Stack<Integer> stack = new Stack<Integer>();
		for (int i = 0; i < size; i++) {
			while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) {
				int j = stack.pop();
				max = Math.max(max, (stack.isEmpty() ? sums[i - 1] : (sums[i - 1] - sums[stack.peek()])) * arr[j]);
			}
			stack.push(i);
		}
		while (!stack.isEmpty()) {
			int j = stack.pop();
			max = Math.max(max, (stack.isEmpty() ? sums[size - 1] : (sums[size - 1] - sums[stack.peek()])) * arr[j]);
		}
		return max;
	}

 */